package com.qububu.utils;


import javax.servlet.ServletException;

import org.directwebremoting.Container;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.event.ScriptSessionEvent;
import org.directwebremoting.event.ScriptSessionListener;
import org.directwebremoting.extend.ScriptSessionManager;
import org.directwebremoting.servlet.DwrServlet;

import com.qububu.common.user.dto.UserInfoDto;

public class InitScriptSession extends DwrServlet{

	private static final long serialVersionUID = -2987777006538182661L;
	
	@Override
	public void init() throws ServletException {
		Container con = ServerContextFactory.get().getContainer();
		ScriptSessionManager sm = con.getBean(ScriptSessionManager.class);
		
		ScriptSessionListener ssl = new ScriptSessionListener() {
			public void sessionCreated(ScriptSessionEvent ev) {
				if (WebContextFactory.get().getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME) != null) {
					String userId =((UserInfoDto)WebContextFactory.get().getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME)).getUserId();
					ev.getSession().setAttribute("userId", userId);
				}
			}
			
			public void sessionDestroyed(ScriptSessionEvent ev) {
//				System.out.println("a ScriptSession is distroyed");
			}
			
			
		};
		sm.addScriptSessionListener(ssl);
	}
}
